#include "serialportset.h"
#include "ui_serialportset.h"
Q_GLOBAL_STATIC(SerialPortSet,theInstance);
SerialPortSet::SerialPortSet(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SerialPortSet)
{
    ui->setupUi(this);
    this->setWindowTitle("串口设置界面");
    this->setWindowIcon(QIcon(":res/res/SerialPortSet.ico"));
}

SerialPortSet::~SerialPortSet()
{
    delete ui;
}

SerialPortSet *SerialPortSet::instance()
{
return theInstance;
}

void SerialPortSet::on_pushButtonConfirm_clicked()
{
//if()



extern serialPortBolock serialPortBolockData[2];
extern bool serialPortDataSetStatus[2];
extern bool MainSwitchEnabel;
setSerialPortBolockData();
if(serialPortDataSetStatus[0])
{
    MainSwitchEnabel=true;
    emit sendMainSwitchEnabel(MainSwitchEnabel);
}
for(int i=0;i<=1;i++)
{
    if(serialPortDataSetStatus[i])
    {
    if(i==0)
    {
        qDebug()<<"modsim1"<<endl;
    }
    else{
        qDebug()<<"modsim2"<<endl;
    }
    qDebug()<<"portName:"<<serialPortBolockData[i].PotrName<<endl;
    qDebug()<<"Baudrate:"<<serialPortBolockData[i].Baudrate<<endl;
    qDebug()<<"DataBits"<<serialPortBolockData[i].DataBits<<endl;
    qDebug()<<"StopBits:"<<serialPortBolockData[i].StopBits<<endl;
    qDebug()<<"Parity:"<<serialPortBolockData[i].Parity<<endl;
    }
}


}


void SerialPortSet::on_pushButtonCancel_clicked()
{
    QMessageBox msgBox;
    msgBox.setWindowTitle("Tip");
    msgBox.setText("串口未设置");
    msgBox.setIcon(QMessageBox::Information); // 设置图标类型
    msgBox.addButton("确定", QMessageBox::AcceptRole); // 自定义按钮

    int result = msgBox.exec(); // 显示对话框并等待用户响应
    if (result == QMessageBox::AcceptRole)
    {

     this->close();



    }
    this->close();//关闭串口取消设置；
}

void SerialPortSet::setSerialPortBolockData()
{

   extern serialPortBolock serialPortBolockData[2];//0为modsim1/2为modsim2/
   extern bool serialPortDataSetStatus[2];
  // 获取当前串口设置信息
   int index=ui->comboBoxModsimSelect->currentIndex();
   serialPortBolockData[index].PotrName=ui->comboBoxSerialPort->currentText();
   switch(ui->comboBoxBaudRate->currentIndex())//获取当前波特率索引
   {
   case 0:
       serialPortBolockData[index].Baudrate=QSerialPort::Baud1200;
       break;
   case 1:
       serialPortBolockData[index].Baudrate=QSerialPort::Baud2400;
       break;
   case 2:
       serialPortBolockData[index].Baudrate=QSerialPort::Baud4800;
       break;
   case 3:
       serialPortBolockData[index].Baudrate=QSerialPort::Baud9600;
       break;
   case 4:
       serialPortBolockData[index].Baudrate=QSerialPort::Baud19200;
       break;
   case 5:
       serialPortBolockData[index].Baudrate=QSerialPort::Baud38400;
       break;
   case 6:
       serialPortBolockData[index].Baudrate=QSerialPort::Baud57600;
       break;
   case 7:
       serialPortBolockData[index].Baudrate=QSerialPort::Baud115200;
       break;
   case 8:
       serialPortBolockData[index].Baudrate=QSerialPort::UnknownBaud;
       break;

   }
   switch(ui->comboBoxDataBits->currentIndex())
   {
   case 0:
       serialPortBolockData[index].DataBits=QSerialPort::Data7;
       break;
   case 1:
        serialPortBolockData[index].DataBits=QSerialPort::Data8;
       break;
   case 2:
        serialPortBolockData[index].DataBits=QSerialPort::UnknownDataBits;
       break;


   }
   switch(ui->comboBoxStopBits->currentIndex())
   {
   case 0:
       serialPortBolockData[index].StopBits=QSerialPort::OneStop;
       break;
   case 1:
       serialPortBolockData[index].StopBits=QSerialPort::TwoStop;
       break;
   case 2:
       serialPortBolockData[index].StopBits=QSerialPort::UnknownStopBits;
       break;

   }

   switch(ui->comboBoxParity->currentIndex())
   {
   case 0:
       serialPortBolockData[index].Parity=QSerialPort:: OddParity;
       break;
   case 1:
       serialPortBolockData[index].Parity=QSerialPort:: EvenParity;
       break;
   case 2:
       serialPortBolockData[index].Parity=QSerialPort::UnknownParity;
       break;



   }
   serialPortDataSetStatus[index]=true;

emit sendModsimSerialPortBlockData(index);
//   QMessageBox::information(this,"success","串口设置成功！");
   QMessageBox msgBox;
   msgBox.setWindowTitle("success");
   msgBox.setText("串口设置成功");
   msgBox.setIcon(QMessageBox::Information); // 设置图标类型
   msgBox.addButton("确定", QMessageBox::AcceptRole); // 自定义按钮

   int result = msgBox.exec(); // 显示对话框并等待用户响应
   if (result == QMessageBox::AcceptRole)
   {

    this->close();



   }

       // 用户点击了“确定”




}

